Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio visualization helpers #474

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

hiroshihorie
Copy link
Member

No description provided.

vDSP_meanv(ptr, 1, &mean, UInt(count))
}
return mean
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simplify this function using ArraySlice to give responsibility for safe pointer arithmetic back to swift instead:

    @inline(__always) private func _computeAverage(_ array: ArraySlice<Float>) -> Float {
        var mean: Float = 0
        array.withUnsafeBufferPointer { bufferPtr in
            vDSP_meanv(bufferPtr.baseAddress!, 1, &mean, vDSP_Length(array.count))
        }
        return mean
    }

then you call it above with _computeAverage(magnitudes[magsStartIdx..<magsEndIdx])

let count = stopIdx - startIdx
array.withUnsafeBufferPointer { bufferPtr in
let ptr = bufferPtr.baseAddress! + startIdx
vDSP_meanv(ptr, 1, &mean, UInt(count))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vDSP_Length(count)

public let scaleType: ScaleType

private let bufferHalfSize: Int
private let bufferLog2Size: Int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should consider declaring these int types as vDSP_Length instead. At the very least, they should be UInt here and cast to vDSP_Length when passed to accelerate later

deinit {
vDSP_destroy_fftsetup(fftSetup)
realPointer.deallocate()
imaginaryPointer.deallocate()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason you're managing raw pointers in this class rather than just using arrays and nested withUnsafeMutableBufferPointer calls as in the apple sample code? looks scary to me 🤷 https://developer.apple.com/documentation/accelerate/vdsp/fast_fourier_transforms/finding_the_component_frequencies_in_a_composite_sine_wave

import Foundation

// Simple ring-buffer used for internal audio processing. Not thread-safe.
class FloatRingBuffer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't really matter but you could call this RingBuffer<T: Numeric> and replace all of the Float with T below and it would still work as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants